﻿using ITXHWEB.Models;
using ReflectionStudio.Core.Events;
using System;
using System.Data;
using System.Web.Mvc;
using WPFExcelReport;
using WPFExcelReport.Helpers;
using ITXHWEB.Infrastructure;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace ITXHWEB.Controllers
{
    public partial class ABFOLGEController
    {
        [ChildActionOnly]
        public ActionResult SABPWV(string returnUrl)
        {
            ModelState.Clear();
            ViewBag.ReturnUrl = returnUrl;
            if (!MABF.IsABPReady)
            {
                INFO = "请扫描有效的单号";
            }
            return PartialView(MABF);
        }
        [HttpPost]
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        public ActionResult SABPWV(ABFOLGEModel model, string returnUrl)
        {
            try
            {
                ModelState.Clear();
                MABF.ABPAUF = model.ABPAUF.Trim();
                WVLOAD(model);
                INFO = "设备扫描正确";
            }
            catch (Exception ex)
            {
                INFO = ex.Message;
                Tracer.Error(ex.StackTrace, ex);
            }
            return RedirectToAction(MAPP, MAPP);
            //return RedirectToAction("ITXWB06", "ITXWB06", new { TempData = TempData });
            //return View(model);
        }
        public void WVLOAD(ABFOLGEModel model)
        {

            MABF.IsABPReady = false;
            if (model.ABPAUF == null)
                return;
            if (model.ABPAUF.Trim().Length < 8)
            {
                MABF.IsABPReady = false;
                return;
            } 
            model.ABPAUF = model.ABPAUF.Trim().PadRight(11);
            PM["KRS"] = model.ABPAUF.Substring(0, 2);
            PM["LFD"] = model.ABPAUF.Substring(2, 6);
            PM["POS"] = model.ABPAUF.Substring(8, 3);
            SReult = ABP.LOADABF(PM);           
            LOADABFS(SReult.Tables[0]);
            //此处可以自动定位到第一条未结束的
            var result = ABFS.Where(p => p.STATUS == " ").OrderByDescending(p => p.FSTEP).OrderBy(p => p.FSTEP);
            foreach (var m in result)
            {
                m.MACHINEID = MABF.MACHINEID;
                m.MAM = MABF.MAM;
                STEPCHK(m);
                break;
            }
              
        }
    }
}
